using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Thinxoft.HttpLib;
using System.IO;
using System.Net.NetworkInformation;
using Thinxoft.XedusLive.SiteFinder;

namespace Thinxoft.XedusLive
{
	/// <summary>
	/// Summary description for Register.
	/// </summary>
	public class Register : System.Windows.Forms.UserControl
	{
		private System.Windows.Forms.TextBox txtEmail;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Button btnRegister;
		private System.Windows.Forms.TextBox txtPassword;
		private System.Windows.Forms.TextBox txtConfirm;
        private System.Windows.Forms.TextBox txtError;
        XedusSiteFinder finder;

        /// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
        NetworkInterface ni;
        //p2p.P2PWebService pws = new p2p.P2PWebService();

		public Register()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

            finder = new XedusSiteFinder();
			//this.Show();

            



        }

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.txtEmail = new System.Windows.Forms.TextBox();
            this.txtPassword = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label9 = new System.Windows.Forms.Label();
            this.txtConfirm = new System.Windows.Forms.TextBox();
            this.label11 = new System.Windows.Forms.Label();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.btnRegister = new System.Windows.Forms.Button();
            this.txtError = new System.Windows.Forms.TextBox();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // txtEmail
            // 
            this.txtEmail.Location = new System.Drawing.Point(132, 26);
            this.txtEmail.Name = "txtEmail";
            this.txtEmail.Size = new System.Drawing.Size(190, 20);
            this.txtEmail.TabIndex = 0;
            // 
            // txtPassword
            // 
            this.txtPassword.Location = new System.Drawing.Point(132, 53);
            this.txtPassword.Name = "txtPassword";
            this.txtPassword.PasswordChar = '*';
            this.txtPassword.Size = new System.Drawing.Size(190, 20);
            this.txtPassword.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(10, 27);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(100, 23);
            this.label1.TabIndex = 10;
            this.label1.Text = "Email";
            // 
            // label9
            // 
            this.label9.Location = new System.Drawing.Point(10, 52);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(100, 23);
            this.label9.TabIndex = 18;
            this.label9.Text = "Password";
            // 
            // txtConfirm
            // 
            this.txtConfirm.Location = new System.Drawing.Point(132, 80);
            this.txtConfirm.Name = "txtConfirm";
            this.txtConfirm.PasswordChar = '*';
            this.txtConfirm.Size = new System.Drawing.Size(190, 20);
            this.txtConfirm.TabIndex = 2;
            // 
            // label11
            // 
            this.label11.Location = new System.Drawing.Point(9, 76);
            this.label11.Name = "label11";
            this.label11.Size = new System.Drawing.Size(100, 23);
            this.label11.TabIndex = 21;
            this.label11.Text = "Confirm";
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Controls.Add(this.btnRegister);
            this.groupBox1.Controls.Add(this.txtPassword);
            this.groupBox1.Controls.Add(this.label9);
            this.groupBox1.Controls.Add(this.txtEmail);
            this.groupBox1.Controls.Add(this.label11);
            this.groupBox1.Controls.Add(this.txtConfirm);
            this.groupBox1.Location = new System.Drawing.Point(6, 5);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(328, 139);
            this.groupBox1.TabIndex = 14;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Required Information";
            // 
            // btnRegister
            // 
            this.btnRegister.Location = new System.Drawing.Point(247, 106);
            this.btnRegister.Name = "btnRegister";
            this.btnRegister.Size = new System.Drawing.Size(75, 23);
            this.btnRegister.TabIndex = 4;
            this.btnRegister.Text = "Register";
            this.btnRegister.Click += new System.EventHandler(this.btnRegister_Click);
            // 
            // txtError
            // 
            this.txtError.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
            this.txtError.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtError.Location = new System.Drawing.Point(434, 22);
            this.txtError.Multiline = true;
            this.txtError.Name = "txtError";
            this.txtError.Size = new System.Drawing.Size(271, 133);
            this.txtError.TabIndex = 25;
            // 
            // Register
            // 
            this.Controls.Add(this.txtError);
            this.Controls.Add(this.groupBox1);
            this.Name = "Register";
            this.Size = new System.Drawing.Size(337, 212);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
		#endregion

		private void groupBox2_Enter(object sender, System.EventArgs e)
		{
		
		}

		private void btnRegister_Click(object sender, System.EventArgs e)
		{
            if (this.txtEmail.Text.IndexOfAny("@!`~}{()*&^%$#'\",?\\[]".ToCharArray()) == -1)
            {

                System.Media.SoundPlayer player = new System.Media.SoundPlayer("media/sounds/tada.wav");
                player.LoadAsync();
                finder.RegisterUserCompleted += delegate(object s, RegisterUserCompletedEventArgs args)
                {

                    

                    if (args.Result)
                    {
                        Start.Email = this.txtEmail.Text;
                        Start.XedusState.XedusSettings.LastEmail = Start.Email;

                        player.PlaySync();
                        MessageBox.Show("You have successfully registered");
                        
                    }
                    else
                    {
                        MessageBox.Show("Either your username or password is incorrect");
                    }
                };
                finder.RegisterUserAsync(this.txtEmail.Text, this.txtPassword.Text,Start.Identity);
            }
            else
            {
                MessageBox.Show("Name cannot contain any special characters");
            }

		}
        
		void FinishRegister(IAsyncResult res)
		{
            //bool result = pws.EndRegisterIp(res);

            //if(result)
            //{
            //    MessageBox.Show("You are now registered with the New Online World");
            //}
            //else
            //{
            //    MessageBox.Show("Could not register you at this time.  Please try again later");
            //}	
	
            //this.Close();
		}
	}
}
